$(function(){
	dictionaryStart("model","21001");
	dictionaryStart("productionfactory","21002");
	setTimeout(function(){
		gtStart(companyId);
	},100);
	$('#linecode').bind('change',function(){
		fallow();
	});
	$('#predevicetype').bind('change',function(){
		gtStart(companyId);
	});
	picStart("24003");
});

function gtStart(companyId){
	$.ajax({
		url : contextPath+"user/dyxl/dlist",
		data : {
			companyId : companyId,
			devicetype : byqtype,
			transformercode : transformercode
		},
		type : 'POST',
		success:function(list){
			//alert(list.length);
			if(null!=list && list.length>0){
				$("#predevicecode").empty().show();
				var startdevicetype=$('#predevicetype').val();
				var starttype="";
				if(startdevicetype==4){
					starttype='dygt';
				}else if(startdevicetype==16){
					starttype='dypdx';
				}else if(startdevicetype==17){
					starttype='dydlfjx';
				}else if(startdevicetype==26){
					starttype='dyyhjrd';
				}
				for(var i=0;i<list.length;i++){
					$.ajax({
						url : contextPath+'user/'+starttype+'/dlist',
						type : 'post',
						data : {
							companyId : companyId,
							deviceid : list[i].deviceid
						},
						success : function(data){
							if(data!=null&&data.length>0){
								var option="";
								for(var j=0;j<data.length;j++){
									option += '<option value="'+data[j].deviceid+'">'+data[j].devicename+'</option>';
								}
								$('#predevicecode').append(option);
								if(predevicecode)
									$('#predevicecode').val(predevicecode);
								option="";
							}
						}
					});
				}
			}
		}
	});
}

function linelist(){
	$.ajax({
		url : contextPath+'user/dyxl/dlist',
		data : {
			companyId : companyId,
			devicetype : byqtype,
			transformercode : transformercode
		},
		type : 'post',
		success : function(data){
			if(data!=null&&data.length>0){
				$("#linecode").empty().show();
				var option="";
				for(var i=0;i<data.length;i++){
					option += '<option value="'+data[i].deviceid+'">'+data[i].devicename+'</option>';
				}
				$("#linecode").append(option);
				$('#linecode').val(deviceid);
			}
		}
	});
}

function fallow(){
	var linecode=$('#linecode').val();
	$.ajax({
		url : contextPath+'user/dyxl/load',
		type : 'post',
		data : {
			deviceid : deviceid
		},
		success : function(data){
			$('#localcity').val(data.localcity);
			$('#operationunit').val(data.operationnit);
			$('#maintenanceteam').val(data.maintenanceteam);
			$('#voltagelevel').val(data.voltagelevel);
		}
	});
}

function uploadPic(){
	var imgcount=$(".img_p").length+1;
	var pic='picfile';
	var picfile=$('#'+pic).val();
	var deviceName=$('#devicename').val();
	if(picfile==''){
		alert('请添加设备图片');
		return;
	}
	var photoType=picfile.substring(picfile.lastIndexOf("."),picfile.length).toUpperCase();
	//alert(photoType);
	if(photoType!=".JPG"&&photoType!=".GIF"){
		alert("添加的图片格式错误");
		return;
	}
	if(deviceName==''){
		alert("请输入设备名称");
		return;
	}
	if(imgcount>6){
		alert("添加图片已达极限");
		return;
	}
	changePicWidth();
	var pictype=$('#pictype').val();
	$.ajaxFileUpload({
		/*url : contextPath+"user/picture/upload?devicename="+deviceName+"&pictype="+pictype+"&imgcount="+imgcount,*/
		url : contextPath+"user/picture/upload",
		secureuri : false,
		fileElementId : pic,
		data : {data : [$('#'+pic).attr('name')],
				devicename : deviceName,
				pictype : pictype,
				imgcount : imgcount
		},
		dataType : 'json',
		contentType: "application/x-www-form-urlencoded; charset=utf-8",
		success : function(data,status){
			//alert(data);
			if(data!=null){
				//alert(data.msgDesc);
				//$('#inner_box').css('width',120*imgcount+'px');
				var option="";
				option += '<div class="img_p"><img class="small_pic" id="number'+imgcount+'" src="'+basePath+'upload/'+data.resourcepicname+'" alt="" />';
				option +='<img class="img_del" onclick="imgdel(this)" src="'+basePath+'user/images/add_members_close.png" />';
				//option += '<div class="img_p"><div class="top_div"><img class="small_pic" onload="autoSize(this,98)" alt="" />';
				option += '<p>'+data.msg+'</p></div>';
				$('#clear').before(option);
				//alert(kzsx_img_cnt);
				if(imgcount>3){
					var kzsx_img_left=-120*(imgcount-3);
					$('.kzsx_cont_leftbox .img_flash_box .center .inner_box').stop().animate({'left':kzsx_img_left+'px'},200);
				}
			}
		}
	});
}

function add(){
	var devicename=$("#devicename").val();
	if(devicename==""){
		alert('设备名称为空');
		return;
	}
	if(!validateLon($(":input[name='longitude']").val())){
		alert("经度范围为-180到180");
		return;
	}
	if(!validateLat($(":input[name='latitude']").val())){
		alert("纬度范围为-90到90");
		return;
	}
	if($("#predevicecode").val()=="0"){
		$("#predevicecode").val("")
	}
	/*if($("input[name='localcity']").val()==""){
		alert("所属地市为空");
		return;
	}
	if($("input[name='operationunit']").val()==""){
		alert("运维单位为空");
		return;
	}
	if($("input[name='maintenanceteam']").val()==""){
		alert("维护班组为空");
		return;
	}
	if(!isVoltage($("input[name='voltagelevel']").val())){
		alert("电压等级不正确,如1kv或220v");
		return;
	}
	if($("#commissioningdate").val()==""){
		alert("投运日期为空");
		return;
	}*/
	/*if(!isVoltage($("input[name='ratedvoltage']").val())){
		alert("额定电压格式错误，如6kv或220v");
		return;
	}
	if(!isNum($("input[name='ratedcurrent']").val())){
		alert("额定电流必须为小数");
		return;
	}*/
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/dybx/add",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			var count=1;
			if($('#inner_box .img_p').length>0){
				$('#inner_box .img_p p').each(function(){
					var picname=$(this).text();
					var linecode=$('#linecode').val();
					var resourcepicname=$("#number"+count).attr("src").substring($("#number"+count).attr("src").lastIndexOf("/")+1);
					var devicecode=data.deviceid;
					var devicename=$('#devicename').val();
					//alert(picname);
					$.ajax({
						url : contextPath+'user/picture/add',
						type : 'post',
						async: false,
						data : {
							picname : picname,
							resourcepicname : resourcepicname,
							linecode : linecode,
							deviceid : devicecode,
							devicename : devicename,
							devicetype : 24
						},
						success : function(data){
							//alert(data.msgDesc);
							count++;
						}
					});
				});
			}
			location.href=contextPath+"user/d/dybxlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
		}
	});
}

function edite(){
	var did=$("input[name='deviceid']").val();
	setStatus(did);
	var devicename=$("#devicename").val();
	if(devicename==""){
		alert('设备名称为空');
		return;
	}
	if(!validateLon($(":input[name='longitude']").val())){
		alert("经度范围为-180到180");
		return;
	}
	if(!validateLat($(":input[name='latitude']").val())){
		alert("纬度范围为-90到90");
		return;
	}
	/*if($("#predevicecode").val()==""){
		alert("请选择上联设备");
		return;
	}
	if($("input[name='localcity']").val()==""){
		alert("所属地市为空");
		return;
	}
	if($("input[name='operationunit']").val()==""){
		alert("运维单位为空");
		return;
	}
	if($("input[name='maintenanceteam']").val()==""){
		alert("维护班组为空");
		return;
	}
	if(!isVoltage($("input[name='voltagelevel']").val())){
		alert("电压等级不正确,如1kv或220v");
		return;
	}
	if($("input[name='commissioningdate']").val()==""){
		alert("投运日期为空");
		return;
	}*/
	/*if(!isVoltage($("input[name='ratedvoltage']").val())){
		alert("额定电压格式错误，如6kv或220v");
		return;
	}
	if(!isNum($("input[name='ratedcurrent']").val())){
		alert("额定电流必须为小数");
		return;
	}*/
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/dybx/edite",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				if($('#inner_box .img_p').length>0){
					$('#inner_box .img_p p').each(function(){
						var picname=$(this).text();
						var linecode=$('#linecode').val();
						var devicecode=data.deviceid;
						//alert($(this).parent().children(".small_pic").attr("src").substring($(this).parent().children(".small_pic").attr("src").lastIndexOf("/")+1));
						var resourcepicname=$(this).parent().children(".small_pic").attr("src").substring($(this).parent().children(".small_pic").attr("src").lastIndexOf("/")+1);
						var devicename=$('#devicename').val();
						var pid=$(this).parent().children(".pid").val();
//						alert(pid);
						if(typeof(pid)=='undefined'){
							pid="";
						}
						//alert(picname);
						$.ajax({
							url : contextPath+'user/picture/edite',
							type : 'post',
							async: false,
							data : {
								picname : picname,
								resourcepicname : resourcepicname,
								linecode : linecode,
								deviceid : devicecode,
								devicename : devicename,
								pid : pid,
								devicetype : 24
							},
							success : function(){
							}
						});
					});
					$.ajax({
						url : contextPath+'user/picture/delete',
						type : 'post',
						async:false,
						data :{
							deviceid : data.deviceid
						},
						success : function(){
							location.href=contextPath+"user/d/dybxlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
						}
					});
				}else{
					$.ajax({
						url : contextPath+'user/picture/delete',
						type : 'post',
						async:false,
						data :{
							deviceid : data.deviceid
						},
						success : function(){
							location.href=contextPath+"user/d/dybxlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
						}
					});
				}
			}
		}
	});
}

function setDefault(elementId,data){
	$('#'+elementId).val(data);
}